package com.example.fw;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.GroupObject;

public class GroupHelper extends HelperWithDriverBase {
	
	public GroupHelper (ApplicationManager manager) {
		super (manager);
	}

	public Set<GroupObject> getGroups() {
		manager.getNavigationHelper().gotoABGroupsPage();
		if (findElements(By.tagName("form")).size() < 2) {
			driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
		}			
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		Set<GroupObject> groups = new HashSet<GroupObject>();
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			//
			title = title.substring("Select (".length(), title.length()-")".length());
			//
			GroupObject group = new GroupObject()
				.setName(title)
				.setId(checkbox.getAttribute("value"));
			groups.add(group);
		}
		return groups;
	}
	
	public void createGroup(GroupObject groupObject) {

		manager.getNavigationHelper().gotoABGroupsPage();
		initNewGroupCreation();
		fillNewGroupFormWithData(groupObject);
		submitNewGroupForm();
		returnToGroupsPage();
	}

	public void deleteGroup(int i) {
		manager.getNavigationHelper().gotoABGroupsPage();
		checkGroup(i);
		click(By.name("delete"));
		returnToGroupsPage();
	}
	
	public void modifyGroup(int i, GroupObject groupObject) {
		manager.getNavigationHelper().gotoABGroupsPage();
		initNewGroupModification(i);
		fillNewGroupFormWithData(groupObject);
		submitGroupModification();
		returnToGroupsPage();
	}

	private void fillNewGroupFormWithData(GroupObject groupObject) {
		type(By.name("group_name"), groupObject.groupName);
		type(By.name("group_header"), groupObject.groupHeader);
		type(By.name("group_footer"), groupObject.groupFooter);
	}
	
	// protected void checkRandomCheckbox() {
    //List<WebElement> checkboxes = driver
      //.findElements(By.name("selected[]"));
       // int checkboxesQuantity = checkboxes.size();
       // int randomCheckbox = rnd.nextInt(checkboxesQuantity);
        //checkboxes.get(randomCheckbox).click();
	
	private void initNewGroupModification(int i) {
		checkGroup(i); 
		click(By.name("edit"));
	}

	private void submitGroupModification() {
		click(By.name("update"));
	}
	
	private void initNewGroupCreation() {
		if ((isElementPresent(By.name("selected[]")) != true)); {
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			}
		click(By.name("new"));
	}
	
	private void submitNewGroupForm() {
		click(By.name("submit"));
	}
	
	private void checkGroup(int i) {
		String path = MessageFormat.format(".//*[@id=''content'']/form[2]/input[{0}]", i);
		if ((isElementPresent(By.name("selected[]")) != true)); {
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			}
        click(By.xpath(path));
	}
	
	private void returnToGroupsPage() {
		click(By.linkText("group page"));
	}
}